<template>
  <div class="item-one" ref="itemOne">
    <div class="title">
      <span>业态交易统计</span>
      <div>
        <el-button type="primary" :disabled='exxxx' @click="bdstatisticexcel()"><i class="el-icon-download" ></i>导出</el-button>
        <el-button icon="el-icon-refresh"></el-button>
      </div>
    </div>
    <div class="container">
      <div class="screeningBox">
        <div class="leftBox">
          <div class="leftitem">
            <div class="block">
              <el-date-picker
                v-model="date"
                type="daterange"
                align="right"
                unlink-panels
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :clearable = 'false'
                :default-time="['00:00:00', '23:59:59']"
                :picker-options="pickerOptions2">
              </el-date-picker>
            </div>
          </div>
          <div class="leftitem" v-if="userinfo.rolesId != 4" >
            <el-form ref="conditions" :model="conditions">
              <el-select  style="width:150px" v-if="userinfo.rolesId == 1 || userinfo.rolesId == 8 || userinfo.rolesId == 9" class="servicelevel" v-model="conditions.servicelevel" placeholder="服务商级别" @change="servicelevel">
                <el-option keys="1" label="一级服务商" :value="1"></el-option>
                <el-option keys="2" label="二级服务商" :value="2"></el-option>
              </el-select>
              <el-autocomplete style="width:200px" v-if="userinfo.rolesId == 1 || userinfo.rolesId == 8 || userinfo.rolesId == 2 || userinfo.rolesId == 9" class="servicename" v-model.trim="conditions.servicename"  :fetch-suggestions="querySearch" placeholder="服务商名称" @select="serviceid"></el-autocomplete>
            </el-form>
          </div>
        </div>
        <div class="rightBox">
          <div class="rightitem" v-if="userinfo.rolesId != 5">
            <el-form ref="conditions" :model="conditions" label-width="80px">
             <el-input class="merchants" v-model.trim="conditions.keywords" placeholder="业态名称/编号"></el-input>
            </el-form>
          </div>
          <div class="rightitem">
            <el-button icon="el-icon-search" type="primary" @click="soso">搜索</el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="reset">重置</el-button>
          </div>
        </div>
      </div>
      <div class="stitle">合计:</div>
       <div class="statistical">
        <div class="statisticalitem">
            <div>总交易金额</div>
            <div><span class="red">{{datainfo.totalFee/100}}</span>元</div>
        </div>
         <div class="statisticalitem">
            <div>总交易笔数</div>
            <div><span class="red">{{datainfo.total}}</span>笔</div>
        </div>
        <div class="statisticalitem">
            <div>总分润</div>
            <div><span class="red">{{datainfo.profit/100}}</span>元</div>
        </div>
      </div>
      <div class="tableBox">
        <el-table
          ref="multipleTable"
          :data="merchantarr"
          tooltip-effect="dark"
          style="width: 100%"
          stripe
          :header-cell-style="tableHeaderColor"
          v-loading="loading"
        >
         <el-table-column align="center" type="index" label="序号"></el-table-column>
         <el-table-column prop="bdName" label="业态名称"></el-table-column>
         <el-table-column prop="bdCode" label="业态编号"></el-table-column>
         <el-table-column prop="totalFee" label="交易总金额">
            <template slot-scope="scope">
              {{scope.row.totalFee/100}}
            </template>
         </el-table-column>
         <el-table-column prop="wxTotalFee" label="微信交易总额">
            <template slot-scope="scope">
              {{scope.row.wxTotalFee/100}}
            </template>
         </el-table-column>
         <el-table-column prop="aliTotalFee" label="支付宝交易总额">
           <template slot-scope="scope">
              {{scope.row.aliTotalFee/100}}
            </template>
         </el-table-column>
         <el-table-column prop="uniTotalFee" label="云闪付交易总额">
           <template slot-scope="scope">
              {{scope.row.uniTotalFee/100}}
            </template>
         </el-table-column>
         <el-table-column prop="bankTotalFee" label="银行卡交易总额">
           <template slot-scope="scope">
              {{scope.row.bankTotalFee/100}}
            </template>
         </el-table-column>
         <el-table-column prop="total" label="交易总笔数"></el-table-column>
         <el-table-column prop="wxTotalCount" label="微信交易笔数"></el-table-column>
         <el-table-column prop="aliTotalCount" label="支付宝交易笔数"></el-table-column>
         <el-table-column prop="uniTotalCount" label="云闪付交易笔数"></el-table-column>
         <el-table-column prop="bankTotalCount" label="银行卡交易笔数"></el-table-column>
         <el-table-column prop="profit" label="总分润"></el-table-column>
          <!-- <el-table-column fixed="right" label="操作" width="120">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="joininfo(scope.row.merchantId)">详细</el-button>
            </template>
          </el-table-column> -->
        </el-table>
        <div class="pagination">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            @size-change="sizechange"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="combined"
            :current-page.sync="cur_page"
          ></el-pagination>
        </div>
      </div>
      <br>
      <br>
      
    </div>
  </div>
</template>

<script>
import { getBdStatistics, getservice,getBD,bdstatisticexcel} from "@/api/api";
export default {
  name:"baseform",
  data: function() {
    return {
      type:'',
      exxxx: false,
      tableData: [],
      cur_page: 1,
      cur_size: 10,
      servicearr:[],
      merchantarr:[],
      combined:10,
      pageCount:'',
      datainfo:{
        totalFee:'',
        fee:'',
        total:'',
        balance:'',
        agent1Profit:'',
        agent2Profit:'',  
        bdProfit:'',
      },
      userinfo:"",
      date:"",
      loading: true,
      conditions: {
        servicelevel: "",
        stardate:"",
        enddate:"",
        serviceid:"",
        aisle:"",
        shopno:"",
        keywords:"",
        servicename:""
      },
      pickerOptions2: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
    };
  },
  created() {
     this.userinfo = JSON.parse(sessionStorage.getItem('userInfo'))
    this.$store.dispatch("getNewNum", 6);
    // console.log(this.$store.state.changableNum);
    // console.log(this.$store.state.showFooter);
    // console.log(this.$store.state.logininfo);
    const start = new Date();
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
    const year=start.getFullYear();
    const month=start.getMonth()+1;
    const date=start.getDate();
    const date1 = year+'-'+month+ '-'+date
    const date2 = year+'-'+month+ '-'+date
    this.date = [date1,date2]
  
    this.getBdStatistics('1')
    if(this.userinfo.rolesId == 1){
      this.conditions.servicelevel = 1
      this.servicelevel('1')
    }
    if(this.userinfo.rolesId == 2){
      this.conditions.servicelevel = 2
      this.servicelevel('2')
    }
    
  },
  methods: {
   
     //修改thead 背景色
     tableHeaderColor({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
          return "background-color: #F0F0F0;font-weight: 500;font-size:14px;color:#000;";
        }
      },
    // 选择服务商 
    serviceid(item){
      this.conditions.serviceid = item.id
    },
    // 分页导航
    handleCurrentChange(val) {
        if(this.conditions.servicelevel == 1){
        this.getBdStatistics('1')
      }else if(this.conditions.servicelevel == 2){
        this.getBdStatistics('2')
      }else{
        this.getBdStatistics('2')
      }
      this.cur_page = val;
       this.$nextTick(() => {
         document.querySelectorAll('.content')[0].scrollTop = '0'
      });

    },
    // 分页数量
    sizechange(val){
      this.cur_size = val
       if(this.conditions.servicelevel == 1){
        this.getBdStatistics('1')
      }else if(this.conditions.servicelevel == 2){
        this.getBdStatistics('2')
      }else{
        this.getBdStatistics('2')
      }
       
       this.$nextTick(() => {
         document.querySelectorAll('.content')[0].scrollTop = '0'
      });
       
    },
    //  重置
    reset() {
      this.conditions.servicelevel =  ''
      this.conditions.servicename =  ''
      this.conditions.stardate = ''
      this.conditions.enddate = ''
      this.cur_page = 1
      this.conditions.keywords = ''
      this.conditions.serviceid = ''
      if(this.userinfo.rolesId == 2){
        this.conditions.servicelevel =  2
      }
      },
   
    // 搜索
    soso(){
      this.loading =true
       this.cur_page = 1
      if(this.conditions.servicelevel == 1){
        this.getBdStatistics('1')
      }else if(this.conditions.servicelevel == 2){
        this.getBdStatistics('2')
      }else{
        this.getBdStatistics('2')
      }
      
    },
    // 服务商名称
     querySearch(queryString, cb) {
      let servicearr = this.servicearr;
      let results = queryString
        ? servicearr.filter(this.createFilter(queryString))
        : servicearr;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) !==
          -1
        );
      };
    },
    // 服务商级别
    servicelevel(val){
      this.servicearr = []
       getservice({
          type: val,
        }).then(res => {
          console.log(res);
          for(var i=0;i<res.data.rows.length;i++){
            this.servicearr.push({'value':res.data.rows[i].agentname,'id':res.data.rows[i].id})
          }
        });

    },
  
    // 业态交易统计
    getBdStatistics(level){

      // console.log(this.conditions)
      let sosolist = {}
      if(level == 1){
        sosolist ={
          page: this.cur_page,
          size: this.cur_size,
          agent1Id: this.conditions.serviceid,
          startDate: this.date[0],
          endDate: this.date[1],
          keywords: this.conditions.keywords
        }
      }else if(level ==2){
        sosolist = {
          page: this.cur_page,
          size: this.cur_size,
          agent2Id: this.conditions.serviceid,
          startDate: this.date[0],
          endDate: this.date[1],
          keywords: this.conditions.keywords
        }
      }
    //   console.log(sosolist)
      getBdStatistics(sosolist).then(res => {
          console.log(res);
          if(res.code == 405){
           sessionStorage. removeItem('token')
            this.$router.push('/login');
          }else if(res.code == 200){
            this.merchantarr = res.data.statisticList.list
            this.datainfo = res.data
            this.combined = res.data.statisticList.pageInfo.recordCount
            this.pageCount = res.data.statisticList.pageInfo.pageCount
            this.loading = false
          }else if(res.code == 500){
            this.$message.error(res.message);
          }
          
        });
    },
    // 导出
    bdstatisticexcel(){
      this.eeeex = true;
      const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      let level = this.conditions.servicelevel;
      let sosolist = {}
      if(level == 1){
        sosolist ={
          page: this.cur_page,
          size: this.cur_size,
          agent1Id: this.conditions.serviceid,
          startDate: this.date[0],
          endDate: this.date[1],
          keywords: this.conditions.keywords
        }
      }else if(level ==2){
        sosolist = {
          page: this.cur_page,
          size: this.cur_size,
          agent2Id: this.conditions.serviceid,
          startDate: this.date[0],
          endDate: this.date[1],
          keywords: this.conditions.keywords
        }
      }else{
        sosolist = {
          page: this.cur_page,
          size: this.cur_size,
          agent2Id: this.conditions.serviceid,
          startDate: this.date[0],
          endDate: this.date[1],
          keywords: this.conditions.keywords
        }
      }
       bdstatisticexcel(sosolist).then(res => {
          if(res.code == 405){
            sessionStorage. removeItem('token')
            this.$router.push('/login');
             loading.close();
             this.exxxx = false;
          }else if(res.code == 200){
            let elink = document.createElement('a');
            elink.href = res.data;
            elink.click();
            this.exxxx = false;
            loading.close();
            
          }else{
            this.$message.error(res.message);
            loading.close();
            this.exxxx = false;
          }
        })


    }
  }
};
</script>
<style scoped lang='less'>
.title {
  background: #fff;
  padding: 10px 10px;
  font-size: 18px;
  font-weight: 500;
  display: flex;
  justify-content: space-between;
}
.name {
  width: 25%;
}
.screeningBox {
  width: 100%;
  display: flex;
}
.leftBox,
.rightBox {
  width: 50%;
  padding: 0 15px;
  box-sizing: border-box;
}
.leftBox > .leftitem {
  margin-bottom: 15px;
  width: 100%;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.rightBox > .rightitem {
  margin-bottom: 15px;
}
.merchants {
  width: 50%;
}
.tableBox {
  margin-top: 20px;
}
.servicename{
  width: 43%;
}
.servicename1{
  width: 60%;
}
.servicelevel{
  width: 23.5%;
}
.width20{
    width: 20%;
}
.red {
  color: #f9607d;
}
.blue{
    color: #01AAEF;
}
.stitle{
    padding: 5px 0;
    font-size: 18px;
}
.stitle{
    padding: 5px 0;
    font-size: 18px;
}
.statistical {
  width: 100%;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background: #374459;
  color: #fff;
  padding: 12px;
  box-sizing: border-box;
  .statisticalitem{
      flex: 1;
      text-align: center;
      border-right: 1px solid rgba(255, 255, 255, 0.445);
      font-size: 13px;
      line-height: 180%;
      &:nth-last-child(1){
          border-right: none;
      }
      .red {
        color: #f9607d;
        font-size: 18px;
      }
      .blue{
            color: #01AAEF;
            font-size: 18px;
        }
  }
}
</style>